package com.ruoyi.web.controller.source;


import cn.hxzy.common.ServiceType;
import cn.hxzy.common.util.RandomId;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.source.entity.Goods;
import com.hxzy.source.service.GoodsService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mingqian.Wu
 * @since 2023-06-26
 */
@RestController
@RequestMapping("/goods")
@DataSource(DataSourceType.SOURCE)
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("getGoodsList/{pageIndex}/{pageSize}")
    public AjaxResult getGoodsList(@PathVariable Integer pageIndex, @PathVariable Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Goods> list = goodsService.list();
        list.stream().forEach(item->{
            item.setServiceTypeName(ServiceType.getServiceTypeValue(item.getServiceType()));
        });
        PageInfo<Goods> pageInfo = new PageInfo<>(list);
        return AjaxResult.success(list).put("total", pageInfo.getTotal());
    }

    @PostMapping("searchGoods/{pageIndex}/{pageSize}")
    public AjaxResult searchGoods(@PathVariable Integer pageIndex,@PathVariable Integer pageSize,@RequestBody Goods goods){
        PageHelper.startPage(pageIndex, pageSize);
        List<Goods> list = goodsService.searchGoods(goods);
        list.stream().forEach(item->{
            item.setServiceTypeName(ServiceType.getServiceTypeValue(item.getServiceType()));
        });
        PageInfo<Goods> pageInfo = new PageInfo<>(list);
        return AjaxResult.success(list).put("total", pageInfo.getTotal());
    }

    @GetMapping("/getGoodsInfo/{id}")
    public AjaxResult getGoodsInfo(@PathVariable String id){
        Goods byId = goodsService.getById(id);
        return AjaxResult.success(byId);
    }

    @PostMapping("/addGoods")
    public AjaxResult addGoods(@RequestBody Goods goods){
        goods.setGoodsId(RandomId.getGoods());
        goods.setCreateTime(new Date());
        boolean save = goodsService.save(goods);
        if (save){
            return AjaxResult.success("新建成功");
        }
        return AjaxResult.error("新建失败");
    }

    @PutMapping("/updateGoods")
    public AjaxResult updateGoods(@RequestBody Goods goods){
        UpdateWrapper<Goods>updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("goods_id",goods.getGoodsId());
        boolean update = goodsService.update(goods, updateWrapper);
        if (update){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }


    @DeleteMapping("/deleteGoods/{id}")
    public AjaxResult deleteGoods(@PathVariable String id){
        boolean b = goodsService.removeById(id);
        if (b){
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }

}

